package com.xzy.mybatis.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzy.mybatis.entity.User;
import com.xzy.mybatis.mapper.UserMapper;
import org.springframework.stereotype.Service;

/**
 * 说明：
 *
 * @author xzy
 * @date 2022/12/7  21:16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /**
     * 新增或编辑
     *
     * @param user -
     * @return -
     * @implNote MyBatis-Plus提供的saveOrUpdate方法会忽略null值，导致null值无法更新进数据库，本方法用于弥补这个问题
     */
    @Override
    public boolean saveOrUpdateWithNull(User user) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(User::getUsername, user.getUsername())
                .set(User::getPassword, user.getPassword())
                .set(User::getRemark, user.getRemark())
                .eq(User::getUserId, user.getUserId());
        return this.saveOrUpdate(user, updateWrapper);
    }
}
